package com.jxj.GUI;

import com.jxj.mapper.mapperUser;
import com.jxj.pojo.student;
import com.jxj.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class newmain {

    private ArrayList<student> studentList = new ArrayList<>();
    private DefaultTableModel model = new DefaultTableModel(
            new Object[]{"学生编号", "姓名", "年龄", "专业", "性别", "本学期公里数", "有效公里数", "跑步总时长", "平均配速", "不合格公里数"}, 0
    ) {
        @Override
        public boolean isCellEditable(int row, int column) {
            return false; // 禁止所有单元格编辑
        }
    };

    private JTextArea textArea = new JTextArea();
    JButton queryButton = new JButton("查询");
    private JScrollPane scrollPane = new JScrollPane();
    private JTable table = new JTable(model);
    private JTextField queryField;
    private JComboBox<String> dropdown;
    JButton addButton;
    JButton deleteButton;
    JButton changeButton;
    private boolean teflag = false;
    private boolean wenflag = false;
    private boolean shoufalg = false;
    private boolean setfalg = false;

    private SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
    mapperUser mapperuser = sqlSession.getMapper(mapperUser.class);
    JFrame frame = new JFrame("智慧东方校园跑管理系统V1.0");

    public void newmain1() {
        int DIALOG_WHITE = 1100;
        int DIALOG_HEIGHT = 600;
        Point point = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
        frame.setBounds(point.x - DIALOG_WHITE / 2, point.y - DIALOG_HEIGHT / 2, DIALOG_WHITE, DIALOG_HEIGHT);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setIconImage(new ImageIcon("C:\\Users\\Lenovo\\Desktop\\icon.png").getImage());
        // 设置窗口为全屏模式
        frame.setUndecorated(true); // 隐藏标题栏和边框
        frame.setVisible(true);
        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout());
        JPanel topPanel = new JPanel(new GridBagLayout());
        topPanel.setPreferredSize(new Dimension(400, 50));
        topPanel.setBackground(new Color(0, 0, 0, 255)); // 标题行背景色
        JLabel systemLabel = new JLabel("校园跑管理系统");
        GridBagConstraints gbc = new GridBagConstraints();
        // 设置组件在垂直方向上的对齐方式为居中
        gbc.gridy = 0; // 设置组件在网格中的纵向位置为0
        // 添加标签到顶部面板，并应用布局参数
        topPanel.add(systemLabel, gbc);
        systemLabel.setFont(new Font("宋体", Font.BOLD, 20));
        systemLabel.setForeground(Color.WHITE); // 标题文字颜色设置为白色
        topPanel.add(systemLabel);
        mainPanel.add(topPanel, BorderLayout.NORTH);
        frame.add(mainPanel);

        JPanel leftPanel = new JPanel(new GridLayout(10, 1));
        leftPanel.setBackground(new Color(0, 0, 0, 176));
        leftPanel.setBorder(BorderFactory.createEmptyBorder(20, 0, 10, 0)); // 添加边距
        JButton studentButton = new JButton("学生基本信息管理");
        studentButton.setForeground(Color.WHITE); // 设置文字颜色为白色
        studentButton.setBackground(new Color(0, 90, 141, 255));
        studentButton.setBorderPainted(false); // 不绘制按钮边框
        studentButton.setFocusPainted(false);
        studentButton.setRolloverEnabled(false);
        studentButton.setPressedIcon(null);
        JButton teacherButton = new JButton("运动数据管理");
        teacherButton.setForeground(Color.WHITE); // 设置文字颜色为白色
        teacherButton.setBackground(new Color(0, 90, 141, 255)); // 设置背景色
        teacherButton.setBorderPainted(false); // 不绘制按钮边框
        teacherButton.setFocusPainted(false);
        teacherButton.setRolloverEnabled(false);
        teacherButton.setPressedIcon(null);
        JButton otherButton = new JButton("问题反馈");
        otherButton.setForeground(Color.WHITE);
        otherButton.setBackground(new Color(0, 90, 141, 255));
        otherButton.setBorderPainted(false);
        otherButton.setFocusPainted(false);
        otherButton.setRolloverEnabled(false);
        otherButton.setPressedIcon(null);
        //JButton userbutton = new JButton("系统设置");
        //leftbutton(userbutton);
        JButton shouye = new JButton("系统首页");
        leftbutton(shouye);
        studentButton.setPreferredSize(new Dimension(150, 30));
        teacherButton.setPreferredSize(new Dimension(150, 30));
        otherButton.setPreferredSize(new Dimension(150, 30));
        leftPanel.add(shouye);
        JLabel label0 = new JLabel();
        Dimension labelSize0 = new Dimension(label0.getPreferredSize().width, 20);
        leftPanel.add(label0);
        label0.setPreferredSize(labelSize0);
        leftPanel.add(studentButton);
        JLabel label1 = new JLabel();
        Dimension labelSize = new Dimension(label1.getPreferredSize().width, 20);
        leftPanel.add(label1);
        label1.setPreferredSize(labelSize);
        leftPanel.add(teacherButton);
        JLabel label2 = new JLabel();
        Dimension labelSize2 = new Dimension(label2.getPreferredSize().width, 20);
        leftPanel.add(label2);
        label2.setPreferredSize(labelSize2);
        leftPanel.add(otherButton);
        JLabel label3 = new JLabel();
        Dimension labelSize3 = new Dimension(label3.getPreferredSize().width, 20);
        leftPanel.add(label3);
        label3.setPreferredSize(labelSize3);
        //leftPanel.add(userbutton);
        mainPanel.add(leftPanel, BorderLayout.WEST);

        //卡片布局
        JPanel content = new JPanel();
        CardLayout cardLayout = new CardLayout();
        content.setLayout(cardLayout);
        JPanel studentPanel = new JPanel();//学生
        JPanel tePanel = new JPanel();//运动
        JPanel settingsPanel = new JPanel();//反馈
        JPanel shouyePanel = new JPanel();//首页
        //JPanel shezhiPanel = new JPanel();
        //shezhiPanel.add(new JLabel("系统设置"));
        content.add(shouyePanel, "shouye");
        // content.add(shezhiPanel, "shezhi");
        content.add(studentPanel, "stu");
        content.add(tePanel, "te");
        content.add(settingsPanel, "other");//反馈
        mainPanel.add(content, BorderLayout.CENTER);
        /*userbutton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cardLayout.show(content, "shezhi");
            }
        });*/
        shouye.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cardLayout.show(content, "shouye");
                if (shoufalg == false) {
                    shouyeGUI s1 = new shouyeGUI();
                    s1.shouGUI(shouyePanel);
                    shoufalg = true;
                }
            }
        });
        studentButton.addActionListener(
                new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        cardLayout.show(content, "stu");
                    }
                }
        );
        shouye.doClick();
        teacherButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cardLayout.show(content, "te");
                if (teflag == false) {
                    teGUI t1 = new teGUI();
                    t1.tePanelGUI(tePanel);
                    teflag = true;
                }
            }
        });
        otherButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cardLayout.show(content, "other");
                if (setfalg == false) {
                    setGUI s1 = new setGUI();
                    s1.setGUI(settingsPanel);
                    setfalg = true;
                }
            }
        });

        //学生管理界面
        JPanel topsearch = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10));
        JLabel shutu = new JLabel("筛选条件:");
        queryField = new JTextField();
        queryField.setPreferredSize(new Dimension(200, 27));
        queryField.setBorder(null);
        addButton = new JButton("添加");
        deleteButton = new JButton("删除");
        changeButton = new JButton("修改");
        changeButton(addButton);
        changeButton(deleteButton);
        changeButton(queryButton);
        changeButton(changeButton);
        String[] options = {"姓名", "学号", "专业", "年龄", "性别"};
        dropdown = new JComboBox<>(options);
        dropdown.setBorder(BorderFactory.createEmptyBorder());
        dropdown.setSelectedIndex(1);
        dropdown.setPrototypeDisplayValue("Thisaaaa");
        // dropdown.setOpaque(false);
        topsearch.add(shutu);
        topsearch.add(dropdown);
        topsearch.add(queryField);
        topsearch.add(queryButton);
        topsearch.add(addButton);
        topsearch.add(deleteButton);
        topsearch.add(changeButton);
        caozuo();
        studentPanel.add(topsearch, BorderLayout.NORTH);
        scrollPane.add(table);
        scrollPane.setPreferredSize(new Dimension(1000, 500)); // 设置滚动面板的首选大小
        scrollPane.setViewportView(table);
        studentPanel.add(scrollPane, BorderLayout.CENTER);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH); // 最大化窗口
    }

    public void leftbutton(JButton Button) {
        Button.setForeground(Color.WHITE);
        Button.setBackground(new Color(0, 90, 141, 255));
        Button.setBorderPainted(false);
        Button.setFocusPainted(false);
        Button.setRolloverEnabled(false);
        Button.setPressedIcon(null);
    }

    public void caozuo() {
        //添加业务
        addButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                add a1 = new add();
                a1.addWindow();
            }
        });
        //删除
        deleteButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    deletestu();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        //修改
        changeButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = table.getSelectedRow();
                if (row == -1) {
                    JOptionPane.showMessageDialog(
                            frame, // 父组件，如果为 null，则弹窗会显示在屏幕中央
                            "当前没有选中任何行", // 提示消息
                            "提示", // 标题
                            JOptionPane.INFORMATION_MESSAGE // 提示类型，这里使用信息类型
                    );
                } else {
                    try {
                        if (hei()) {
                            JOptionPane.showMessageDialog(
                                    frame, // 父组件，如果为 null，则弹窗会显示在屏幕中央
                                    "黑名单用户无法修改数据,请先移除黑名单", // 提示消息
                                    "提示", // 标题
                                    JOptionPane.INFORMATION_MESSAGE // 提示类型，这里使用信息类型
                            );
                        } else {
                            change ch1 = new change();
                            ch1.changeWindow(table);
                        }
                    } catch (SQLException ex) {
                        throw new RuntimeException(ex);
                    }
                }

            }
        });
        //查询
        queryButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                search();
            }
        });
    }

    //判断是否为黑名单用户
    private boolean hei() throws SQLException {
        int row = table.getSelectedRow();
        int click = (int) table.getValueAt(row, 0);
        return mapperuser.hei(click);
    }

    private void deletestu() throws SQLException {
        int result = showConfirmDeleteDialog();
        if (result == JOptionPane.YES_OPTION) {
            int row = table.getSelectedRow();
            int click = (int) table.getValueAt(row, 0);
            mapperuser.deletestu1(click);
            mapperuser.deletestu2(click);
            queryButton.doClick();
        }
    }

    public static int showConfirmDeleteDialog() {
        // 显示确认删除的对话框，并返回用户的选择
        return JOptionPane.showConfirmDialog(null, "确认删除选中的行？", "确认删除", JOptionPane.YES_NO_OPTION);
    }

    public void changeButton(JButton Button) {
        Button.setBorderPainted(false); // 不绘制按钮边框
        Button.setFocusPainted(false);
        Button.setRolloverEnabled(false);
        Button.setPressedIcon(null);
        Button.setForeground(Color.WHITE); // 设置文字颜色为白色
        Button.setBackground(new Color(0, 90, 141, 255)); // 设置背景色
        Button.setPreferredSize(new Dimension(100, 30));
    }

    public void newmodel(JTable table) {
        table.setRowHeight(30);
        model = new DefaultTableModel(
                new Object[]{"学生编号", "姓名", "年龄", "专业", "性别", "本学期公里数", "有效公里数", "跑步总时长", "平均配速", "不合格公里数"}, 0
        ) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false; // 禁止所有单元格编辑
            }
        };
        table.setModel(model);

        // 其他表格设置（可选）
        table.getColumnModel().getColumn(0).setPreferredWidth(100); // 设置第一列宽度
        table.getColumnModel().getColumn(1).setPreferredWidth(150); // 设置第二列宽度
        // 你可以根据需要继续设置其他列的宽度
    }

    public void search() {
        String selectedItem = (String) dropdown.getSelectedItem();
        String text = queryField.getText();

        if (text.equals("all")) {
            List<student> studentList = mapperuser.getstudentList();
            System.out.println(studentList);

            // 清空表格
            model.setRowCount(0);

            for (student student : studentList) {
                model.addRow(new Object[]{
                        student.getStudent_id(),
                        student.getName(),
                        student.getAge(),
                        student.getMajor(),
                        student.getGrade(),
                        student.getBKM(),
                        student.getYKM(),
                        student.getAlltime(),
                        student.getAvgspeed(),
                        student.getNOKM()
                });
            }
        }
    }
}

